// pages/recruitMine/recruitMine.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imgUrl: app.globalData.imageUrl,
    userInfo:'',
    bottabData: app.globalData.bottabDataForZp,
    infoList:[
      { label: '简历名称', key: '', url: '/pages/enter/enter?page=10', key:'resumeName'},
      { label: '个人信息', key: '', url: '/pages/recruitPersonalInfo/recruitPersonalInfo', key: 'name'},
      { label: '求职意向', key: '', url: '/pages/recruitIntention/recruitIntention', key: 'jobName' },
      { label: '教育背景', key: '', url: '/pages/recruitEduback/recruitEduback?page=1', key: 'isEducation'},
      { label: '工作/实习经历', key: '', url: '/pages/recruitEduback/recruitEduback?page=2', key: 'isWork'}, 
      { label: '项目经验', key: '', isoptional: true, url: '/pages/recruitEduback/recruitEduback?page=3', key: 'isProject' }, 
      { label: '自我评价', key: '', isoptional: true, url: '/pages/jobDescrip/jobDescrip?page=3', key: 'selfEvaluate'},
      { label: '切换身份', key: 'userCharacter', url: '/pages/recruitChangeIdentity/recruitChangeIdentity'},
    ],
    myIsRcruit:false,
    detail:'',
    wholeMine:false,
    isFirst:true,
    loadSuccess:false
  },
  switchTo:function(e){
    var item=e.currentTarget.dataset.item;
    if(item.url){
      wx.navigateTo({
          url: item.url
      })
    }
  },
  addPulish: function (e) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.page =='wholeMine'){
      this.setData({
        wholeMine:true,
        myIsRcruit: options.myIsRcruit=='false'?false:true
      })
      if (this.data.myIsRcruit){
        wx.setNavigationBarTitle({
          title: '我的招聘',
        })
      }else{
        wx.setNavigationBarTitle({
          title: '我的求职',
        })
      }
    }
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['bottabData[1].current']: true,
    })
    this.getUserIdentity(true);
    wx.showLoading();
  },
  getUserIdentity: function (flag) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getOne', //仅为示例，并非真实的接口地址
      data: { userId: this.data.userInfo.id },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          that.setData({
            isFirst: false
          })
          if (res.data.data) {
            if (!that.data.wholeMine){
              that.setData({
                myIsRcruit: res.data.data.userCharacter == '招聘' ? true : false
              })
            }
            that.setData({
              detail: res.data.data,
              ['infoList[0].url']: '/pages/enter/enter?page=10&value=' + (res.data.data.resumeName ? res.data.data.resumeName : '') + '&id=' + res.data.data.id,
              ['infoList[6].url']: '/pages/jobDescrip/jobDescrip?page=3&value=' + (res.data.data.selfEvaluate ? res.data.data.selfEvaluate : '') + '&id=' + res.data.data.id,
            })
            if (that.data.myIsRcruit){
              if (!that.data.detail.onCompanyName) {
                if (flag) {
                  wx.navigateTo({
                    url: '/pages/recruitBasicInfo/recruitBasicInfo',
                  })
                } else {
                  if (that.data.wholeMine) {
                    wx.navigateBack({
                      delta: 1
                    })
                  } else {
                    wx.redirectTo({
                      url: '/pages/recruitHome/recruitHome',
                    })
                  }
                }
              }
            } 
          }else{
            if (that.data.wholeMine) {
              if (that.data.myIsRcruit) {
                that.setUserIdentity('招聘');
              }
            }else{
              that.setUserIdentity('求职');
            }
          } 
        }
      }
    })
  },
  setUserIdentity: function (value) {//设置用户身份
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/save', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: { userId: this.data.userInfo.id, user_character: value },
      success: function (res) {
        if (res.data.success) {
          that.getUserIdentity();
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  changePicture:function(e){
    var that = this;
    wx.showActionSheet({
      itemList: ['更换头像'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.chooseImage({
            count: 1, // 默认9
            success: function (res) {
              that.upload(res.tempFilePaths);
            }
          })
        }
      },
      fail: function (res) {
      }
    })
  },
  /*上传图片*/
  upload: function (path) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
      url: app.globalData.httpApiCar +'uploadfile/upload1',
        filePath: path[0],
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {},
        success: function (res) {
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var url = JSON.parse(res.data).data;
          that.setPicture(url);
        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  setPicture: function (value){
    var that=this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/update', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        id: this.data.detail.id,
        headPicture: value
      },
      success: function (res) {
        if (res.data.success) {
          that.getUserIdentity();
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!this.data.isFirst){
      this.getUserIdentity(false);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})